package com.ft.core.enums;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @copyright (C), 2015-2019, XXX有限公司
 * @fileName: EnumIntValueSerializer
 * @author: 李阳
 * @date: 2019/6/28 1:55
 * @description: IEnumIntValue枚举类型序列化器
 * @history: <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public class EnumIntValueSerializer implements ObjectSerializer {

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        if (IEnumIntValue.class.isAssignableFrom((Class) fieldType)) {
            final int value = ((IEnumIntValue) object).getValue();
            out.writeInt(value);
        } else {
            new JSONSerializer().write(object);
        }

    }
}
